package net.peihuan.controller;


import net.peihuan.entity.Tag;
import net.peihuan.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/tag")
public class TagController {

    @Autowired
    private TagService tagService;

    @RequestMapping("/getAllTags")
    @ResponseBody
    public List<Tag> getAllTags(){
        return tagService.findAll();
    }

    @RequestMapping("/saveTag")
    @ResponseBody
    public int savaTag(@RequestBody Tag tag){

        return tagService.save(tag);
    }

    @RequestMapping("/selectTag")
    @ResponseBody
    public Tag selectTag(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("id"));
        return tagService.findById(id);
    }

    @RequestMapping("/selectAllTagsWithArticles")
    @ResponseBody
    public List<Tag> selectAllTagsWithArticles(){
        return tagService.findAllWithArticles();
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(@RequestBody Tag tag){
        if(tagService.update(tag)==1){
            return "success";
        }
            return "failed";
    }

    //查询标签的个数
    @RequestMapping("/count")
    @ResponseBody
    public int countTag(){
        return tagService.count();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(HttpServletRequest request){
        int id=Integer.parseInt(request.getParameter("id"));
        if(tagService.delete(id)==1){
            return"success";
        }else{
            return "failed";
        }
    }
}
